package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.ActivityDTO;
import edu.whu.demo.domain.Entry;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.whu.demo.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author
 * @since 2022-12-06
 */
@Mapper
public interface EntryDao extends BaseMapper<Entry> {

    @Select("select uid from entry where activity_id=#{activityId}")
    List<Long> getUids(Long activityId);

    @Select("select u.* from user u INNER JOIN entry e " +
            "on u.uid=e.uid ${ew.customSqlSegment}")
    IPage<User> getMembers(IPage<User> page,
                           @Param(Constants.WRAPPER) QueryWrapper<User> qw);

    @Select("select a.* from activity a INNER JOIN entry e " +
            "on a.id=e.activity_id " +
            "${ew.customSqlSegment}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(
                    //此处uid不需要写为m.uid和u.uid是因为多表查询是在@Select查询完之后在实体类基础上进行的
                    property = "leastUnsuccessfulStage", column = "id",
                    javaType = Integer.class,
                    one = @One(select = "edu.whu.demo.dao.ActivityMemberDao.getLeastSuccessfulStage")
            )
    })
    IPage<ActivityDTO> getActivityByEntry(Page<ActivityDTO> page,
                                          @Param(Constants.WRAPPER) QueryWrapper<ActivityDTO> qw);


}
